Skip to main content

🎲 Unit 5: Arrays Made Simple

Welcome back, eager learner! πŸ‘‹
Today we’re exploring Arrays β€” a magical way of keeping lots of values in one tidy place.
Instead of juggling variables everywhere, arrays let you line them up neatly like books on a shelf. πŸ“š


🌟 Why Use Arrays?​

Imagine This

Without arrays, managing 100 student marks means writing 100 separate variables. 😡
With arrays, you just say: β€œHey, here’s one variable that can hold 100 values!”

int student1 = 85;
int student2 = 90;
int student3 = 78;
int student4 = 92;
int student5 = 88;
// Imagine managing 100 students!

πŸ”‘ Key Characteristics​

PropertyDescription
Fixed SizeDecide size at declaration
Same Data TypeAll elements must be of same type
Contiguous MemoryStored side by side in memory
Zero-IndexedFirst element is at index 0
Random AccessAccess any element directly

🎯 One-Dimensional Arrays​

πŸ“œ Declaration Syntax​

datatype array_name[size];

// Examples
int numbers[10]; // Array of 10 integers
float prices[50]; // Array of 50 floats
char name[30]; // Array of 30 characters (string)

🌱 Initialization Styles​

int marks[5] = {85, 90, 78, 92, 88};

🧠 Memory Representation​

Array: int arr[5] = {10, 20, 30, 40, 50};

Memory:
Address: 1000 1004 1008 1012 1016
β”Œβ”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
Value: β”‚ 10 β”‚ 20 β”‚ 30 β”‚ 40 β”‚ 50 β”‚
β””β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜
Index: 0 1 2 3 4

πŸ›  Accessing Array Elements​

#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50};

printf("First element: %d\n", arr[0]);
printf("Last element: %d\n", arr[4]);

arr[2] = 35;
printf("Modified third element: %d\n", arr[2]);

printf("\nAll elements:\n");
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}

return 0;
}

πŸ“ Input and Output with Arrays​

#include <stdio.h>

int main() {
int n, marks[50];

printf("Enter number of students: ");
scanf("%d", &n);

for (int i = 0; i < n; i++) {
printf("Student %d: ", i + 1);
scanf("%d", &marks[i]);
}

printf("\nMarks entered:\n");
for (int i = 0; i < n; i++) {
printf("Student %d: %d\n", i + 1, marks[i]);
}

return 0;
}

πŸ” Searching in Arrays​

int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) return i;
}
return -1;
}

πŸŒ€ Sorting Arrays​

void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

🧡 Strings (Character Arrays)​

char name[50];
char greeting[20] = "Hello";
char message[] = "Welcome to C!";

πŸ”’ Two-Dimensional Arrays​

int arr2D[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

πŸ† Common Problems to Try​

  • πŸ”„ Reverse an array
  • πŸ₯ˆ Find the second largest element
  • 🚫 Remove duplicates

⚠️ Limitations & Best Practices​

  • Size is fixed at compile-time
  • No automatic bounds checking (be careful!)
  • Use constants for array sizes
  • Always initialize arrays to avoid garbage values
#define MAX_SIZE 100
int arr[MAX_SIZE] = {0};

πŸŽ“ Quick Quiz​

What is the index of the first element in a C array?

  • 1
  • 0 βœ…
  • -1